﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Dasboard.DatePicker;
using System.Xml;
using System.Collections.Generic;

namespace Dasboard
{
    public partial class Report : System.Web.UI.Page
    {
        private List<WebControl> comps;
        private Table compTable;
        private int selectService;
        private int counter = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            // Create a instance of the list of controls
            comps = new List<WebControl>();
            compTable = new Table();
            // Check in the request which report is asked.
            if (Request.QueryString["reportID"] != null)
            {
                selectService = Int32.Parse(Request.QueryString["reportID"]);
            }
            // Check if the login is true.
            if (Session["LoggedIn"] != null)
            {
                if ((bool)Session["LoggedIn"] == true)
                {
                    // Start building of the GUI
                    BuildGUI();
                }
            }       
        }

        public DataSet getComponentsService()
        {
            switch (selectService)
            {
                case 8:
                    Date.ReportService8.ReportService service = new Date.ReportService8.ReportService();
                    return service.getComponents();
                case 7:
                    Date.ReportService7.MonthOverviewShippingMethodGroep7 service7 = new Date.ReportService7.MonthOverviewShippingMethodGroep7();
                    return service7.getComponents();
                case 6:
                    Date.ReportService6.AverageProductLifecycle service6 = new Date.ReportService6.AverageProductLifecycle();
                    return service6.getComponents();
            }
            return null;
        }
        public DataSet getDataSetService(DataSet set)
        {
            switch (selectService)
            {
                case 8:
                    Date.ReportService8.ReportService service = new Date.ReportService8.ReportService();
                    return service.getDataset(set);
                case 7:
                    Date.ReportService7.MonthOverviewShippingMethodGroep7 service7 = new Date.ReportService7.MonthOverviewShippingMethodGroep7();
                    return service7.getDataset(set);
                case 6:
                    Date.ReportService6.AverageProductLifecycle service6 = new Date.ReportService6.AverageProductLifecycle();
                    return service6.getDataset(set);
            }
            return null;
        }

        private void BuildGUI()
        {            
            DataSet set = new DataSet();
            bool succeed = true;
            try
            {
                set = getComponentsService();
            }
            catch (Exception e)
            {
                succeed = false;
            }
            if (succeed)
            {
                // load the XmlDocument from the DataSet.    
                XmlDocument xmldom = new XmlDocument();
                xmldom.LoadXml(set.GetXml());

                XmlNodeList source = xmldom.GetElementsByTagName("components");
                //Get all components;
                XmlNodeList components = source[0].ChildNodes;
                foreach (XmlNode component in components)
                {
                    // Check in the switch wich component there is asked.
                    string type = component.Attributes["type"].InnerText;
                    switch (type)
                    {
                        case "label":
                            AddLabel(component.Attributes["label"].InnerText, null);
                            break;
                        case "string":
                            AddTexBox(component);
                            break;
                        case "list":
                            AddCombo(component);
                            break;
                        case "datetime":
                            AddCalender(component);
                            break;
                    }
                }
                PanelComponent.Controls.Add(compTable);
            }
            else
            {
                // If failed with the data, show a line of failing in the GUI.
                Label label = new Label();
                label.Text = "Failed creating connection with the webservice";
                PanelComponent.Controls.Add(label);
            }
        }

        private void AddLabel(string text, TableRow row)
        {
            Label label = new Label();
            label.Text = text;
            if (row == null)
            {
                TableCell cell = new TableCell();
                cell.Controls.Add(label);
                TableRow subrow = new TableRow();
                subrow.Cells.Add(cell);
                compTable.Rows.Add(subrow);
            }
            else
            {
                TableCell cell = new TableCell();
                cell.Controls.Add(label);
                row.Cells.Add(cell);
            }
        }
        private void AddTexBox(XmlNode node)
        {
            // Create a textbox
            TextBox box = new TextBox();
            // Create a row for in the table
            TableRow row = new TableRow();
            // Set the object text.
            AddLabel(node.Attributes["label"].InnerText, row);
            // Create Cell and add the textbox
            TableCell cell = new TableCell();
            cell.Controls.Add(box);
            // Add cell to the row
            row.Cells.Add(cell);
            // Add the box to the list of components.
            comps.Add(box);
            // Add the row to the table.
            compTable.Rows.Add(row);
        }
        private void AddCalender(XmlNode node)
        {
            // Create a textbox
            DateSelector cal = new DateSelector(counter);
            counter++;
            ContentPlaceHolder holder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
            cal.addSetPopup(holder.ClientID+"_"+ cal.txt_Date.ClientID);
            // Create a row for in the table
            TableRow row = new TableRow();
            // Set the object text.
            AddLabel(node.Attributes["label"].InnerText, row);
            // Create Cell and add the textbox
            TableCell cell = new TableCell();
            cell.Controls.Add(cal);
            // Add cell to the row
            row.Cells.Add(cell);
            // Add the box to the list of components.
            comps.Add(cal);
            // Add the row to the table.
            compTable.Rows.Add(row);
        }
        private void AddCombo(XmlNode node)
        {
            // Create a textbox
            DropDownList drop = new DropDownList();
            drop.EnableViewState = true;
            // Add the items to the dropdownlist.
            XmlNodeList items = node.ChildNodes;
            foreach (XmlNode item in items)
            {
                drop.Items.Add(new ListItem(item.Attributes["name"].InnerText, item.Attributes["value"].InnerText));
            }
            // Create a row for in the table
            TableRow row = new TableRow();
            // Set the object text.
            AddLabel(node.Attributes["label"].InnerText, row);
            // Create Cell and add the textbox
            TableCell cell = new TableCell();
            cell.Controls.Add(drop);
            // Add cell to the row
            row.Cells.Add(cell);
            // Add the box to the list of components.
            comps.Add(drop);
            // Add the row to the table.
            compTable.Rows.Add(row);
        }

        protected void ShowButtonClick(object sender, EventArgs e)
        {
            // Create xmldoc for the output to the webservice
            XmlDocument doc = new XmlDocument();
            // Create XML decleration.
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);
            // Create root element.
            XmlNode root = doc.CreateElement("root");
            doc.AppendChild(root);
            // Create Node for what level the user is and add it to the doc.
            XmlNode level = doc.CreateElement("level");
            root.AppendChild(level);
            // Create a child node for 'level' where the value is from the user.           
            XmlNode LevelValue = doc.CreateElement("value");
            LevelValue.InnerText = "3";
            // Add value to the level Node
            level.AppendChild(LevelValue);

            XmlNode values = doc.CreateElement("values");
            root.AppendChild(values);
            // Load all values of the components in to the XML
            foreach (WebControl control in comps)
            {
                if (control is TextBox)
                {
                    TextBox text = (control as TextBox);
                    values.AppendChild(AddValue(text.Text, doc));
                }
                else if (control is DropDownList)
                {
                    DropDownList list = (control as DropDownList);
                    values.AppendChild(AddValue(list.SelectedValue.ToString(), doc));
                }
                else if (control is DateSelector)
                {
                    DateSelector date = (control as DateSelector);
                    values.AppendChild(AddValue(date.txt_Date.Text, doc));
                }
            }
            DataSet set = new DataSet();
            bool succeed = true;
            try
            {                
                DataSet returnData = new DataSet();
                returnData.ReadXml(new XmlNodeReader(doc));
                set = getDataSetService(returnData);
            }
            catch
            {
                succeed = false;
            }
            if (succeed)
            {
                GridView grid = new GridView();
                grid.DataSource = set;
                PanelGrid.Controls.Add(grid);
                grid.DataBind();
            }
            else
            {
                Label label = new Label();
                label.Text = "Fault with the webservice";
                PanelGrid.Controls.Add(label);
            }
        }
        private XmlNode AddValue(string value, XmlDocument doc)
        {
            XmlNode XMLvalue = doc.CreateElement("value");
            XMLvalue.InnerText = value;
            return XMLvalue;
        }
    }
}
